package com.sinochem.yunlian.ship.base.rest.manage;

import com.github.pagehelper.PageInfo;
import com.sinochem.yunlian.ship.base.biz.BsPortBiz;
import com.sinochem.yunlian.ship.base.constants.BsPortConstant;
import com.sinochem.yunlian.ship.base.entity.BsPort;
import com.sinochem.yunlian.ship.base.vo.request.manage.ReqEditPortForm;
import com.sinochem.yunlian.ship.base.vo.request.manage.ReqFindPortListVo;
import com.sinochem.yunlian.ship.base.vo.response.PortVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.DisableStatus;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import com.sinochem.yunlian.ship.common.util.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wanghui
 * @Dscription: 运营港口管理
 * @Date: Created in 17:53 2018/3/5
 **/
@IgnoreToken
@RestController
@RequestMapping("manage/port")
public class BsManagePortController extends BaseController<BsPortBiz,BsPort> {

    @Autowired
    private BsPortBiz bsPortBiz;

    @Autowired
    private IdGenerator idGenerator;


    /**
     * @Auther: wanghui
     * @Dscription: 查询所有启用流域,城市名称
     * @Date: Created in 17:45 2018/3/5
     **/
    @RequestMapping(value = "/findAllName", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse findAllPortsName(){
        RestResponse result = new RestResponse();
        List<PortVo> portVos = new ArrayList<PortVo>();
            List<BsPort> ports = bsPortBiz.findAllPortsName();

            for (BsPort port : ports) {
                if (port.getParentId() == 0 && port.getLevel() == BsPortConstant.LEVLE_1) {
                    PortVo vo = new PortVo();
                    vo.setId(port.getId());
                    vo.setParentId(port.getParentId());
                    vo.setPortName(port.getPortName());
                    portVos.add(vo);
                    for (BsPort portOne : ports) {
                        if (portOne.getLevel() == BsPortConstant.LEVLE_2 && portOne.getParentId().equals(vo.getId())) {
                            PortVo voOne = new PortVo();
                            voOne.setId(portOne.getId());
                            voOne.setParentId(portOne.getParentId());
                            voOne.setPortName(portOne.getPortName());
                            if (vo.getChildrens() == null) {
                                vo.setChildrens(new ArrayList<>());
                            }
                            vo.getChildrens().add(voOne);
                        }
                    }
                }
            }
        result.setData(portVos);
        return result;
    }




    /**
     * @Auther: wanghui
     * @Dscription: 运营管理---港口列表
     * @Date: Created in 20:47 2018/3/8
     **/
    @RequestMapping(value = "/portList", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse findAllPorts(@RequestBody ReqFindPortListVo form){
        RestResponse result = new RestResponse();
        List<PortVo> list = new ArrayList<>();
        Map<String ,Object> map = new HashMap<>();
        if(form.getId() == null){
            //展示所有列表
            PageInfo<BsPort> bsPorts = bsPortBiz.findAllPorts(form);
            for (BsPort bsPort : bsPorts.getList()) {
                PortVo portVo = new PortVo();
                portVo.setId(bsPort.getId());
                portVo.setParentId(bsPort.getParentId());
                portVo.setEnName(bsPort.getEnName());
                portVo.setPortName(bsPort.getPortName());
                portVo.setLat(bsPort.getLat());
                portVo.setLon(bsPort.getLon());
                portVo.setSeqNum(bsPort.getSeqNum());
                portVo.setDisableStatus(bsPort.getDisableStatus());
                list.add(portVo);
            }
            map.put("rows",list);
            map.put("total",bsPorts.getTotal());
        }else {
            BsPort port = bsPortBiz.selectById(form.getId());
            if (form.getId() == 0) {
                //展示所有列表
                PageInfo<BsPort> bsPorts = bsPortBiz.findCity(form);
                for (BsPort bsPort : bsPorts.getList()) {
                    PortVo portVo = new PortVo();
                    portVo.setId(bsPort.getId());
                    portVo.setParentId(bsPort.getParentId());
                    portVo.setEnName(bsPort.getEnName());
                    portVo.setPortName(bsPort.getPortName());
                    portVo.setLat(bsPort.getLat());
                    portVo.setLon(bsPort.getLon());
                    portVo.setSeqNum(bsPort.getSeqNum());
                    portVo.setDisableStatus(bsPort.getDisableStatus());
                    list.add(portVo);
                }
                map.put("rows", list);
                map.put("total", bsPorts.getTotal());
            } else if (port.getParentId() == 0 && port.getLevel() == 1) {
                //展示城市
                PageInfo<BsPort> bsPortList = bsPortBiz.findAllPorts(form);
                for (BsPort bsPort : bsPortList.getList()) {
                    PortVo portVo = new PortVo();
                    portVo.setId(bsPort.getId());
                    portVo.setParentId(bsPort.getParentId());
                    portVo.setEnName(bsPort.getEnName());
                    portVo.setPortName(bsPort.getPortName());
                    portVo.setLat(bsPort.getLat());
                    portVo.setLon(bsPort.getLon());
                    portVo.setSeqNum(bsPort.getSeqNum());
                    portVo.setDisableStatus(bsPort.getDisableStatus());
                    list.add(portVo);
                }
                map.put("rows", list);
                map.put("total", bsPortList.getTotal());
//                    //展示所有港口
//                    List<BsPort> portList = bsPortBiz.findCitysById(port.getId());
//                    List<Long> ids = new ArrayList<>();
//                    for (BsPort port1:portList) {
//                        ids.add(port1.getId());
//                        params.put("ids",ids);
//                    }
//                    PageInfo<BsPort> bsPorts = bsPortBiz.findPort(params);
//                    for (BsPort bsPort : bsPorts.getList()) {
//                        PortVo portVo = new PortVo();
//                        portVo.setId(bsPort.getId());
//                        portVo.setPortCode(bsPort.getPortCode());
//                        portVo.setPortName(bsPort.getPortName());
//                        portVo.setLat(bsPort.getLat());
//                        portVo.setLon(bsPort.getLon());
//                        portVo.setSeqNum(bsPort.getSeqNum());
//                        portVo.setDisableStatus(bsPort.getDisableStatus());
//                        list.add(portVo);
//                    }
//                    map.put("rows",list);
//                    map.put("total",bsPorts.getTotal());
            } else if (port.getLevel() == 2) {
                //展示所有港口
                PageInfo<BsPort> bsPorts = bsPortBiz.findAllPorts(form);
                for (BsPort bsPort : bsPorts.getList()) {
                    PortVo portVo = new PortVo();
                    portVo.setId(bsPort.getId());
                    portVo.setParentId(bsPort.getParentId());
                    portVo.setEnName(bsPort.getEnName());
                    portVo.setPortName(bsPort.getPortName());
                    portVo.setLat(bsPort.getLat());
                    portVo.setLon(bsPort.getLon());
                    portVo.setSeqNum(bsPort.getSeqNum());
                    portVo.setDisableStatus(bsPort.getDisableStatus());
                    list.add(portVo);
                }
                map.put("rows", list);
                map.put("total", bsPorts.getTotal());
            }
        }
        result.setData(map);
        return result;
    }


    /**
     * @Auther: wanghui
     * @Dscription: 根据id获取当前流域下的城市列表
     * @Date: Created in 17:42 2018/3/6
     **/
    @RequestMapping(value = "/findCity/{parentId}",method = RequestMethod.GET)
    public RestResponse findCitysById(@PathVariable(value = "parentId") Long parentId){
        RestResponse response = new RestResponse();
        List<BsPort> bsPorts = bsPortBiz.findCitysById(parentId);
        List<PortVo> portVos = new ArrayList<>();
        for (BsPort bsPort : bsPorts) {
            PortVo vo = new PortVo();
            vo.setId(bsPort.getId());
            vo.setParentId(bsPort.getParentId());
            vo.setPortName(bsPort.getPortName());
            vo.setLevel(bsPort.getLevel());
            portVos.add(vo);
        }
        response.setData(portVos);
        return response;
    }

    /**
     *
     * @Description: 获取港口详情
     */
    @RequestMapping(value = "/portDetail/{id}",method = RequestMethod.GET)
    public RestResponse<HashMap<Object, Object>> selectPortDetail(@PathVariable(value = "id", required = true)Long id) {
        HashMap<Object, Object> hashMap = bsPortBiz.selectPortDetail(id);
        return RestResponse.success(hashMap);
    }


    /**
     * 运营管理---查询港口列表
     * @param vo
     * @return
     */
//    @RequestMapping(value = "/portList",method = RequestMethod.POST)
//    public RestResponse findPortList( @RequestBody ReqFindPortListVo vo) {
//        RestResponse response = new RestResponse();
//        PageInfo<BsPort> bsPorts = bsPortBiz.findPortListByVo(vo);
//        List<PortVo> list = new ArrayList<>();
//        Map<String ,Object> result = new HashMap<>();
//        for (BsPort bsPort : bsPorts.getList()) {
//            PortVo portVo = new PortVo();
//            portVo.setId(bsPort.getId());
//            portVo.setPortCode(bsPort.getPortCode());
//            portVo.setPortName(bsPort.getPortName());
//            portVo.setLat(bsPort.getLat());
//            portVo.setLon(bsPort.getLon());
//            portVo.setSeqNum(bsPort.getSeqNum());
//            portVo.setDisableStatus(bsPort.getDisableStatus());
//            list.add(portVo);
//        }
//        result.put("rows",list);
//        result.put("total",bsPorts.getTotal());
//        response.setData(result);
//        return response;
//    }

    /**
     * 禁用/启用港口
     * @param id
     * @param disableStatus
     * @return
     */
    @RequestMapping(value = "/updatePortStatus",method = RequestMethod.GET)
    public RestResponse setPortStatus(@RequestParam(value = "id", required = true)Long id, @RequestParam(value = "disableStatus", required = true) int disableStatus) {
        if (DisableStatus.STATUS_DISABLE != disableStatus && DisableStatus.STATUS_ENABLE != disableStatus) {
            return RestResponse.caughtException("参数有误");
        }
        BsPort bsPort = bsPortBiz.selectPortById(id);
        if (bsPort == null) {
            return RestResponse.success("无操作数据");
        }
        int maxLevel = bsPortBiz.selectPortMaxLevel();
        bsPortBiz.updatePort(id, disableStatus, bsPort, maxLevel);
        return RestResponse.success("SUCCESS");
}
    /**
     * @Auther: wanghui
     * @Dscription: 添加/修改港口
     * @Date: Created in 19:10 2018/3/7
     **/
    @RequestMapping(value = "/editPort",method = RequestMethod.POST)
    public RestResponse editPortByVo(@RequestBody ReqEditPortForm form){
        bsPortBiz.updateMaterial(form);
        return RestResponse.success("操作成功");
    }

}